var t = getApp();

Page({
  data: {
    edit: !0,
    editContet: "编辑商品",
    selectAllStatus: !1,
    carNumber: !1,
    carList: "",
    imgUrl: "/hyb_yl/shopsub/assets/shop_images/jian01.png",
    carBoo: !0,
    accountBoo: !0,
    promotionList: "",
    promotionBoo: !0,
    moneyOffList: "",
    differBoo: !0,
    carList:[
      {
        id:1,
        moneyOffList:1,
        nmPromTime:'2018-10-26',
        selected:1,
        imageUrl: "/assets/images/796.png",
        name:'维生素',
        price:20000,
        num:10
      },
      {
        id: 1,
        moneyOffList: 1,
        nmPromTime: '2018-10-26',
        selected: 1,
        imageUrl: "/assets/images/796.png",
        name: '维生素',
        price: 20000,
        num: 10
      },
      {
        id: 1,
        moneyOffList: 1,
        nmPromTime: '2018-10-26',
        selected: 1,
        imageUrl: "/assets/images/796.png",
        name: '维生素',
        price: 20000,
        num: 10
      },
      {
        id: 1,
        moneyOffList: 1,
        nmPromTime: '2018-10-26',
        selected: 1,
        imageUrl: "/assets/images/796.png",
        name: '维生素',
        price: 20000,
        num: 10
      },
      {
        id: 1,
        moneyOffList: 1,
        nmPromTime: '2018-10-26',
        selected: 1,
        imageUrl: "/assets/images/796.png",
        name: '维生素',
        price: 20000,
        num: 10
      }
    ],
    totalPrice:10000,
    inTotal:30000,
    
  },
  onLoad: function(t) {},
  onShow: function() {
    // this.setData({
    //   carBoo: !0,
    //   carList: "",
    //   edit: !0,
    //   selectAllStatus: !1,
    //   accountBoo: !0,
    //   promotionBoo: !0
    // });
    // wx.showLoading({
    //     title: "加载中",
    //     mask: !0
    // });
    var o = this,
      e = wx.getStorageSync("logs") || "",
      a = wx.getStorageSync("cartSessionId") || "";
    e || wx.login({
      success: function(e) {
        var a = e.code;
        wx.request({
          url: t.globalData.httpLogin + "user/code",
          data: {
            code: a,
            token: o.data.token
          },
          success: function(t) {
            var e = t.data.data.token;
            if (e) {
              o.getCarList(e);
              try {
                wx.setStorageSync("logs", e);
              } catch (t) {}
            }
          },
          fail: function(t) {
            wx.showToast({
              title: "系统繁忙，请稍后重试",
              icon: "none",
              duration: 3e3
            });
          }
        });
      }
    }), this.setData({
      token: e,
      cartSessionId: a
    }), e && o.getCarList(o.data.token);
  },
  onHide: function() {
    this.setData({
      carList: ""
    });
  },
  onUnload: function() {},
  editFun: function() {
    this.setData({
      edit: !this.data.edit
    });
  },
  selectList: function(t) {
    var o = this,
      e = t.currentTarget.dataset.index,
      a = this.data.carList,
      i = !0,
      n = [],
      s = a[e].selected;
    a[e].selected = !s, a.forEach(function(t, e) {
      t.selected ? (n.push(t.id), a[e].differTextBoo = !1, a[e].moneyOffList && o.setData({
        promotionBoo: !1
      })) : (i = !1, n.splice(e, 1), a[e].moneyOffList && (a[e].promCondition = a[e].moneyOffList[0].promCondition / 100,
        a[e].promValue = a[e].moneyOffList[0].promValue / 100, a[e].nmPromTime = a[e].moneyOffList[0].nmPromTime,
        a[e].satisfyBoo = !1, a[e].differTextBoo = !0, o.setData({
          promotionBoo: !0
        })));
    });
    var r = n.join(",");
    this.setData({
      carList: a,
      selectAllStatus: i,
      itemIds: r
    }), this.getTotalPrice();
  },
  selectAll: function(t) {
    var o = this.data.selectAllStatus,
      e = this.data.promotionBoo;
    o = !o;
    for (var a = this.data.carList, i = 0; i < a.length; i++) a[i].selected = o, a[i].moneyOffList && o ? e = !1 : (e = !0,
      a[i].moneyOffList && (a[i].promCondition = a[i].moneyOffList[0].promCondition / 100,
        a[i].promValue = a[i].moneyOffList[0].promValue / 100, a[i].nmPromTime = a[i].moneyOffList[0].nmPromTime,
        a[i].satisfyBoo = !1));
    this.setData({
      selectAllStatus: o,
      carList: a,
      promotionBoo: e
    }), this.getTotalPrice();
  },
  addCount: function(o) {
    wx.showLoading({
      mask: !0
    });
    var e = this,
      a = o.currentTarget.dataset.index,
      i = o.target.id,
      n = this.data.carList,
      s = n[a].num;
    wx.request({
      url: t.globalData.httpOne + "mall/cart/decreOrIncre",
      data: {
        itemId: i,
        itemCount: 1,
        index: a,
        type: 1,
        cartSessionId: e.data.cartSessionId,
        orderId: "",
        orderType: 0
      },
      success: function(t) {
        200 == t.data.code && (wx.hideLoading(), s += 1, n[a].num = s, e.setData({
          carList: n,
          imgUrl: "/pages/shopsub/assets/shop_images/jian02.png"
        }), e.getTotalPrice());
      },
      fail: function(t) {
        wx.showToast({
          title: "系统繁忙，请稍后重试",
          icon: "none",
          duration: 1e3
        });
      }
    });
  },
  minusCount: function(o) {
    wx.showLoading({
      mask: !0
    });
    var e = this,
      a = o.currentTarget.dataset.index,
      i = o.target.id,
      n = this.data.carList,
      s = n[a].num;
    if (s <= 1) return wx.showToast({
      title: "至少一件商品",
      icon: "none",
      duration: 1e3
    }), !1;
    wx.request({
      url: t.globalData.httpOne + "mall/cart/decreOrIncre",
      data: {
        itemId: i,
        itemCount: 1,
        index: a,
        type: 2,
        cartSessionId: e.data.cartSessionId,
        orderId: "",
        orderType: 0
      },
      success: function(t) {
        200 == t.data.code && (wx.hideLoading(), (s -= 1) <= 1 && e.setData({
          imgUrl: "/pages/shopsub/assets/shop_images/jian01.png"
        }), n[a].num = s, e.setData({
          carList: n
        }), e.getTotalPrice());
      },
      fail: function(t) {
        wx.showToast({
          title: "系统繁忙，请稍后重试",
          icon: "none",
          duration: 1e3
        });
      }
    });
  },
  tapDel: function() {
    var o = this,
      e = [],
      a = 0,
      i = this.data.carList,
      n = [],
      s = "";
    0 != i.length && (i.forEach(function(t) {
      1 == t.selected && (n.push(t.id), a++), s = n.join(",");
    }), a <= 0) ? wx.showToast({
      title: "请先选择商品",
      icon: "none",
      duration: 1e3
    }) : wx.showModal({
      content: "确定要删除该商品吗？",
      confirmColor: "#3b4ca9",
      success: function(a) {
        a.confirm && wx.request({
          url: t.globalData.httpOne + "mall/cart/remove",
          data: {
            itemIds: s,
            cartSessionId: o.data.cartSessionId
          },
          success: function(t) {
            200 == t.data.code && (o.setData({
              promotionBoo: !0
            }), i.forEach(function(t, o) {
              1 == t.selected ? delete i[o] : e.push(t);
            }), 0 == e.length && o.setData({
              carBoo: !1
            }), o.setData({
              carList: e
            })), o.getTotalPrice();
          },
          fail: function(t) {
            wx.showToast({
              title: "系统繁忙，请稍后重试",
              icon: "none",
              duration: 1e3
            });
          },
          complete: function() {}
        });
      }
    });
  },
  getTotalPrice: function() {
    var t = this.data.carList,
      o = 0;
    if (t) {
      for (var e = 0, a = 0, i = 0; i < t.length; i++)
        if (t[i].selected) {
          if (o = t[i].num * t[i].price, a += t[i].num * t[i].price, t[i].moneyOffList) {
            this.setData({
              promotionBoo: !1
            });
            for (var n = 0; n < t[i].moneyOffList.length; n++) console.log(t[i].num * t[i].price / 100),
              console.log(t[i].moneyOffList[n].promCondition / 100), t[i].num * t[i].price / 100 >= t[i].moneyOffList[n].promCondition / 100 ? (console.log(t[i].num * t[i].price / 100),
                console.log(t[i].moneyOffList[n].promCondition / 100), t[i].differTextBoo = !0,
                o = t[i].num * t[i].price - t[i].moneyOffList[n].promValue, t[i].differText = 0,
                t[i].promCondition = t[i].moneyOffList[n].promCondition / 100, t[i].promValue = t[i].moneyOffList[n].promValue / 100,
                t[i].nmPromTime = t[i].moneyOffList[n + 1] ? t[i].moneyOffList[n + 1].nmPromTime : t[i].moneyOffList[n].nmPromTime,
                t[i].satisfyBoo = !0) : t[i].num * t[i].price / 100 < t[i].promCondition && t[i].promCondition == t[i].moneyOffList[0].promCondition / 100 && (t[i].differText = 100 * t[i].promCondition - o,
                t[i].promCondition = t[i].moneyOffList[0].promCondition / 100, t[i].promValue = t[i].moneyOffList[0].promValue / 100,
                t[i].nmPromTime = t[i].moneyOffList[0].nmPromTime, t[i].satisfyBoo = !1, t[i].differTextBoo = !1);
          }
          e += o;
        }
      this.setData({
        carList: t,
        totalPrice: e,
        inTotal: a
      });
    }
  },
  toAccounts: function() {
    var o = this,
      e = [],
      a = [],
      i = [];
    if (this.data.carList.forEach(function(t, o) {
        t.selected && (e.push(t.id), a.push(o), i.push(t.num));
      }), e.length <= 0) wx.showToast({
      title: "请先选择商品",
      icon: "none",
      duration: 1e3
    });
    else {
      var n = e.join(","),
        s = a.join(","),
        r = i.join(",");
      o.data.accountBoo && wx.request({
        url: t.globalData.httpOne + "mall/order/orderInfo",
        data: {
          token: o.data.token,
          cartSessionId: o.data.cartSessionId,
          itemIds: n,
          indexs: s,
          nums: r
        },
        success: function(t) {
          if (200 == t.data.code && t.data.data) {
            try {
              wx.setStorageSync("carData", t.data.data);
            } catch (t) {}
            wx.navigateTo({
              url: "/hyb_yl/shopsub/pages/indent/indent"
            });
          }
        }
      }), o.setData({
        accountBoo: !1
      });
    }
  },
  getNum: function(o, e, a, i) {
    wx.request({
      url: t.globalData.httpOne + "mall/cart/decreOrIncre",
      data: {
        itemId: e,
        itemCount: 1,
        index: a,
        type: o,
        cartSessionId: that.data.cartSessionId
      },
      success: function(t) {},
      fail: function(t) {
        wx.showToast({
          title: "系统繁忙，请稍后重试",
          icon: "none",
          duration: 3e3
        });
      }
    });
  },
  getCarList: function(o) {
    var e = this;
    wx.request({
      url: t.globalData.httpOne + "mall/cart/list",
      data: {
        token: o,
        cartSessionId: e.data.cartSessionId
      },
      success: function(t) {
        if (200 == t.data.code) {
          if (wx.hideLoading(), !t.data.data) return void e.setData({
            carList: "",
            carBoo: !1
          });
          var o = t.data.data;
          o.forEach(function(t, a) {
            t.selected = !1, e.getPromotion(t.id, o, a, e);
          });
        }
      },
      fail: function(t) {
        wx.showToast({
          title: "系统繁忙，请稍后重试",
          icon: "none",
          duration: 3e3
        });
      }
    });
  },
  getPromotion: function(o, e, a, i) {
    wx.request({
      url: t.globalData.httpOne + "mall/prom/itemPromInfo",
      data: {
        itemId: o
      },
      success: function(t) {
        if (200 == t.data.code) {
          var o = t.data.data,
            n = [],
            s = {};
          if (o && o.forEach(function(t) {
              "240000" == t.cdActiveType && n.push(t);
            }), n.length > 0) {
            for (var r = 0; r < n.length; r++) r + 1 < n.length && n[r].promCondition > n[r + 1].promCondition && (s = n[r],
              n[r] = n[r + 1], n[r + 1] = s);
            e[a].moneyOffList = n, e[a].promCondition = n[0].promCondition / 100, e[a].promValue = n[0].promValue / 100,
              e[a].nmPromTime = n[0].nmPromTime;
          }
        }
        i.setData({
          carList: e
        }), i.getTotalPrice(), console.log(i.data.carList);
      }
    });
  }
});